package com.bestom.bluetoothlock.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


import com.bestom.bluetoothlock.LockApplication;
import com.bestom.bluetoothlock.R;
import com.bestom.bluetoothlock.adapter.FingerMangeAdapter;
import com.bestom.bluetoothlock.bluetooth.BleServiceManage;
import com.bestom.bluetoothlock.bluetooth.BraceletInstructions;
import com.bestom.bluetoothlock.entity.DeviceInfo;
import com.bestom.bluetoothlock.entity.FingerData;
import com.bestom.bluetoothlock.entity.FingerInfo;
import com.bestom.bluetoothlock.entity.ReceiveInfo;
import com.bestom.bluetoothlock.util.Constants;
import com.bestom.bluetoothlock.util.CurrencyDataManage;
import com.bestom.bluetoothlock.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FingerprintManageActivity extends BaseActivity {
    /* access modifiers changed from: private */
    public FingerMangeAdapter adapter;
    /* access modifiers changed from: private */
    public BleServiceManage bleManage;
    /* access modifiers changed from: private */
    public boolean delete_all = false;
    /* access modifiers changed from: private */
    public String deleteid = "";
    /* access modifiers changed from: private */
    public DeviceInfo deviceInfo;
    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        
        public void handleMessage(Message message) {
            super.handleMessage(message);
            if (message.what == 1) {
                ReceiveInfo receiveInfo = (ReceiveInfo) message.obj;
                String cmd = receiveInfo.getCmd();
                String result = receiveInfo.getResult();
                if (cmd.equals(BraceletInstructions.INSTRUCTIONS_RESET_FINGER)) {
                    if (result.equals("00")) {
                        if (FingerprintManageActivity.this.isClear) {
                            boolean unused = FingerprintManageActivity.this.isClear = false;
                        } else {
                            ToastUtil.toast(FingerprintManageActivity.this.getString(R.string.delete_success));
                        }
                    } else if (FingerprintManageActivity.this.isClear) {
                        ToastUtil.toast(FingerprintManageActivity.this.getString(R.string.clear_fingerprint_fail));
                        boolean unused2 = FingerprintManageActivity.this.isClear = false;
                    } else {
                        ToastUtil.toast(FingerprintManageActivity.this.getString(R.string.delete_fail));
                    }
                } else if (cmd.equals(BraceletInstructions.INSTRUCTIONS_DELETE_FINGER)) {
                    boolean equals = result.equals("00");
                }
            }
        }
    };
    @SuppressLint("HandlerLeak")
    private Handler handlerListen = new Handler() {

        public void handleMessage(Message message) {
            int i;
            int i2;
            super.handleMessage(message);
            int i3 = message.what;
            if (i3 != 1) {
                if (i3 == 3) {
                    ReceiveInfo receiveInfo = (ReceiveInfo) message.obj;
                    String cmd = receiveInfo.getCmd();
                    String result = receiveInfo.getResult();
                    boolean z = false;
                    if (cmd.equals(BraceletInstructions.INSTRUCTIONS_RESET_FINGER)) {
                        if (result.equals("00")) {
                            String lockmac = CurrencyDataManage.getInstance().getCurrentBleDevice().getLockmac();
                            int i4 = LockApplication.getSharedPreferencesHelper().getInt("finger_count_" + lockmac);
                            if (FingerprintManageActivity.this.delete_all) {
                                FingerprintManageActivity.this.list.clear();
                                FingerprintManageActivity.this.adapter.setData(FingerprintManageActivity.this.list);
                                if (i4 > 0) {
                                    for (int i5 = 0; i5 < i4; i5++) {
                                        StringBuilder sb = new StringBuilder();
                                        sb.append("finger_");
                                        sb.append(lockmac);
                                        sb.append("_name");
                                        sb.append(LockApplication.getSharedPreferencesHelper().getString("finger_" + lockmac + "_id" + i5));
                                        LockApplication.getSharedPreferencesHelper().saveString(sb.toString(), "");
                                    }
                                    LockApplication.getSharedPreferencesHelper().saveInt("finger_count_" + lockmac, 0);
                                }
                            } else {
                                if (i4 > 0) {
                                    i2 = 0;
                                    while (true) {
                                        if (i2 >= i4) {
                                            break;
                                        }
                                        if (FingerprintManageActivity.this.deleteid.equals(LockApplication.getSharedPreferencesHelper().getString("finger_" + lockmac + "_id" + i2))) {
                                            z = true;
                                            break;
                                        }
                                        i2++;
                                    }
                                }
                                i2 = -1;
                                if (z) {
                                    LockApplication.getSharedPreferencesHelper().saveString("finger_" + lockmac + "_name" + FingerprintManageActivity.this.deleteid, "");
                                    int i6 = i4 - 1;
                                    if (i2 != i6) {
                                        int i7 = i2;
                                        while (i7 < i6) {

                                            String str = "finger_" + lockmac + "_id" + i7;

                                            StringBuilder sb2 = new StringBuilder();
                                            sb2.append("finger_");
                                            sb2.append(lockmac);
                                            sb2.append("_id");
                                            i7++;
                                            sb2.append(i7);
                                            LockApplication.getSharedPreferencesHelper().saveString(str, LockApplication.getSharedPreferencesHelper().getString(sb2.toString()));
                                        }
                                    }
                                    LockApplication.getSharedPreferencesHelper().saveInt("finger_count_" + lockmac, i6);
                                    if (FingerprintManageActivity.this.list.size() > i2) {
                                        FingerprintManageActivity.this.list.remove(i2);
                                        FingerprintManageActivity.this.adapter.setData(FingerprintManageActivity.this.list);
                                    }
                                }
                            }
                            ToastUtil.toast(FingerprintManageActivity.this.getString(R.string.delete_success));
                        } else if (FingerprintManageActivity.this.isClear) {
                            ToastUtil.toast(FingerprintManageActivity.this.getString(R.string.clear_fingerprint_fail));
                            boolean unused = FingerprintManageActivity.this.isClear = false;
                        } else {
                            ToastUtil.toast(FingerprintManageActivity.this.getString(R.string.delete_fail));
                        }
                    } else if (cmd.equals(BraceletInstructions.INSTRUCTIONS_DELETE_FINGER) && result.equals("00")) {
                        String lockmac2 = CurrencyDataManage.getInstance().getCurrentBleDevice().getLockmac();
                        int i8 = LockApplication.getSharedPreferencesHelper().getInt("finger_count_" + lockmac2);
                        if (i8 > 0) {
                            i = 0;
                            while (true) {
                                if (i >= i8) {
                                    break;
                                }
                                if (FingerprintManageActivity.this.deleteid.equals(LockApplication.getSharedPreferencesHelper().getString("finger_" + lockmac2 + "_id" + i))) {
                                    z = true;
                                    break;
                                }
                                i++;
                            }
                        }
                        i = -1;
                        if (z) {
                            LockApplication.getSharedPreferencesHelper().saveString("finger_" + lockmac2 + "_name" + FingerprintManageActivity.this.deleteid, "");
                            int i9 = i8 - 1;
                            if (i != i9) {
                                int i10 = i;
                                while (i10 < i9) {
                                    String str2 = "finger_" + lockmac2 + "_id" + i10;
                                    StringBuilder sb3 = new StringBuilder();
                                    sb3.append("finger_");
                                    sb3.append(lockmac2);
                                    sb3.append("_id");
                                    i10++;
                                    sb3.append(i10);
                                    LockApplication.getSharedPreferencesHelper().saveString(str2, LockApplication.getSharedPreferencesHelper().getString(sb3.toString()));
                                }
                            }
                            LockApplication.getSharedPreferencesHelper().saveInt("finger_count_" + lockmac2, i9);
                            if (FingerprintManageActivity.this.list.size() > i) {
                                FingerprintManageActivity.this.list.remove(i);
                                FingerprintManageActivity.this.adapter.setData(FingerprintManageActivity.this.list);
                            }
                            ToastUtil.toast(FingerprintManageActivity.this.getString(R.string.delete_success));
                        }
                    }
                }
            } else if (!((Boolean) message.obj).booleanValue()) {
                ToastUtil.toast(FingerprintManageActivity.this.getString(R.string.device_disconnect));
                FingerprintManageActivity.this.bleManage.connect(FingerprintManageActivity.this.deviceInfo);
                FingerprintManageActivity.this.finish();
            }
        }
    };
    /* access modifiers changed from: private */
    public boolean isClear = false;
    @BindView(R.id.iv_left)
    ImageView iv_left;
    /* access modifiers changed from: private */
    public List<FingerInfo> list = new ArrayList();
    @BindView(R.id.list_friend)
    ListView listView;
    @BindView(R.id.tv_right)
    TextView tv_right;
    @BindView(R.id.tv_title)
    TextView tv_title;

    /* access modifiers changed from: protected */
    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView((int) R.layout.activity_fingerprint_manage);
        ButterKnife.bind((Activity) this);
        this.deviceInfo = (DeviceInfo) getIntent().getSerializableExtra(Constants.KEY_VALUE);
        initView();
    }

    private void initView() {
        this.tv_title.setText(getString(R.string.fingerprint_manage));
        this.iv_left.setVisibility(View.VISIBLE);
        this.tv_right.setText(getString(R.string.fingerprint_clear));
        this.tv_right.setVisibility(View.VISIBLE);
        this.adapter = new FingerMangeAdapter(this);
        this.listView.setAdapter(this.adapter);
        this.bleManage = BleServiceManage.getInstance(this);
        this.bleManage.setHandlerListener(this.handlerListen);
        this.adapter.setOnRightClickListener(new FingerMangeAdapter.OnRightClickListener() {
            public void onRightClick(View view, int i) {
                if (!FingerprintManageActivity.this.bleManage.isConnect()) {
                    ToastUtil.toast(FingerprintManageActivity.this.getString(R.string.not_connected_bluetooth));
                    return;
                }
                FingerInfo fingerInfo = (FingerInfo) FingerprintManageActivity.this.list.get(i);
                String unused = FingerprintManageActivity.this.deleteid = fingerInfo.getFingerid();
                boolean unused2 = FingerprintManageActivity.this.delete_all = false;
                FingerprintManageActivity.this.bleManage.sendData(BraceletInstructions.deleteFingerInstructions(fingerInfo.getFingerid()));
            }
        });
        int i = LockApplication.getSharedPreferencesHelper().getInt("finger_count_" + this.deviceInfo.getLockmac());
        if (i > 0) {
            for (int i2 = 0; i2 < i; i2++) {
                FingerInfo fingerInfo = new FingerInfo();
                fingerInfo.setFingerid(LockApplication.getSharedPreferencesHelper().getString("finger_" + this.deviceInfo.getLockmac() + "_id" + i2));

                String string = LockApplication.getSharedPreferencesHelper().getString("finger_" + this.deviceInfo.getLockmac() + "_name" + fingerInfo.getFingerid(), fingerInfo.getFingerid());
                if (string.equals("")) {
                    fingerInfo.setFingername(fingerInfo.getFingerid());
                } else {
                    fingerInfo.setFingername(string);
                }
                this.list.add(fingerInfo);
            }
            this.adapter.setData(this.list);
        }
    }

    private void setData(FingerData fingerData) {
        if (fingerData.getFingerprintlist() != null) {
            this.list = fingerData.getFingerprintlist();
            this.adapter.setData(this.list);
        }
    }

    /* access modifiers changed from: package-private */
    @OnClick({R.id.iv_left, R.id.tv_right})
    public void onClickView(View view) {
        int id = view.getId();
        if (id == R.id.iv_left) {
            finish();
        } else if (id == R.id.tv_right) {
            if (!this.bleManage.isConnect()) {
                ToastUtil.toast(getString(R.string.not_connected_bluetooth));
                return;
            }
            this.isClear = true;
            this.delete_all = true;
            this.bleManage.sendData(BraceletInstructions.resetFingerInstructions());
        }
    }
}
